layui.config({base: '/manifest/js/',version:'202007141822'}).extend({mftUtil: 'mft-util'}).use(['layer','form','table','element','mftUtil'], function() {
    var layer = layui.layer, form = layui.form,table=layui.table,element = layui.element,mftUtil=layui.mftUtil,
        fn = {
            initData:function(){
                $.post(rurl('declare/declaration/defaultDeclareUnit'),{},function (res) {
                    if (isSuccess(res)) {
                        var data = res.data;
                        if(isNotEmpty(data.name)){
                            form.val('mtfHeadForm',{'unitCode':data.socialNo,'msgRepName':data.name});
                        }
                    }
                });
                mftUtil.init();
            },
            showCheckMsg:function(data){
                if(!data||data.length==0){return;}
                var mgsList = [];
                $.each(data,function (i,o) {
                    mgsList.push((i+1)+'：'+o.message);
                    if(isNotEmpty(o.field)){
                        verificationTips($(o.field),o.message,-1);
                    }
                });
                prompt_warn(mgsList.join('<br/>'));
            },
            packData:function(){
                var manifest = $('#mtfHeadForm').serializeJson();
                var $billTab = $('#mft-bill-area ').find('.layui-table-main').find('tr');
                var billNos = [];
                for(var i=0;i<mftUtil.data.mftBills.length;i++){
                    var item = mftUtil.data.mftBills[i];
                    if(isEmpty(item.billNo)){
                        var $itemEl = $($billTab[item.listNo-1]).find('td[data-field="billNo"]');
                        $itemEl.addClass('layui-form-danger');
                        tipsMessage('提(运)单号不能为空',$itemEl);
                        return;
                    }
                    if(contain(billNos,item.billNo)){
                        var $itemEl = $($billTab[item.listNo-1]).find('td[data-field="billNo"]');
                        $itemEl.addClass('layui-form-danger');
                        tipsMessage('提(运)单号重复',$itemEl);
                        return;
                    }
                    billNos.push(item.billNo);
                }
                return {'manifest':manifest,'mftBills':mftUtil.data.mftBills,'mtfGoods':mftUtil.data.mftGoods,'mftContainers':mftUtil.data.mftContainers};
            },
            saveMtf:function () {
                var params = fn.packData();
                if(params==null){return null;}
                show_loading();
                postJSON('declare/manifest/save',params,function (res){
                    show_success('暂存成功');delayedUrl('manifest/exp/edit.html?id='+res.data,1200);
                });
            },sendSingle:function (isAgain) {
                var id = $('#mtfHeadForm #id').val();
                if (isEmpty(id)) {
                    show_msg('请先暂存舱单数据');
                    return;
                }
                var params = fn.packData();
                if(params==null){return null;}
                show_loading();
                postJSON('declare/manifest/saveAndSend',params,function (res){
                    if(res.data&&res.data.length>0){
                        fn.showCheckMsg(res.data);
                    }else{
                        prompt_success('发送成功',function () {refPage();});
                    }
                });
            }
        };
    fn.initData();
    $('#saveMtf').click(function () {
        fn.saveMtf();
    });
    $('#sendSingle').click(function () {
        fn.sendSingle(false);
    });
});
function fullCustomerData(data) {
    $('#customerId').val(data.id);
}